//
//  YLcontinuationRecordController.m
//  YLFinanceService
//
//  Created by she1323 on 2017/9/14.
//  Copyright © 2017年 she1323. All rights reserved.
//  续投记录

#import "YLContinuationRecordController.h"
#import "YLTitleArrowCell.h"
#import "YLTitleHeader.h"
#import "YLGlobeHeader.h"
#import "YLContinuationDetailController.h"
#import "YLContinuationRecordViewModel.h"
#import "YLContinuationRecordModel.h"
@interface YLContinuationRecordController ()
@property (nonatomic, strong) YLContinuationRecordViewModel *viewModel;
@end

@implementation YLContinuationRecordController

- (YLContinuationRecordViewModel *)viewModel
{
    if (!_viewModel) {
        _viewModel = [[YLContinuationRecordViewModel alloc] init];
    }
    return _viewModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"续投记录";
    //
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([YLTitleArrowCell class]) bundle:nil] forCellReuseIdentifier:[YLTitleArrowCell cellID]];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    __weak typeof(self) weakSelf = self;
    [[self.viewModel.requestCommand execute:nil] subscribeNext:^(id  _Nullable x) {
        weakSelf.tableView.emptyDataSetSource = self;
        weakSelf.tableView.emptyDataSetDelegate = self;
        [weakSelf.tableView reloadData];
    }];
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
    NSString * text = @"暂时还没有续投记录!";
    
    NSDictionary * attributes = @ { NSFontAttributeName:[UIFont boldSystemFontOfSize:14.0f ],
        NSForegroundColorAttributeName:YLLineColor};
    
    return [[ NSAttributedString  alloc ] initWithString:text attributes:attributes];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.viewModel.group.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    YLContinuationRecordModel *model = self.viewModel.group[section];
    return model.item.count;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    YLContinuationRecordModel *model = self.viewModel.group[section];
    YLTitleHeader *header = [YLTitleHeader new];
    header.backgroundColor = [UIColor clearColor];
    header.titleColor = YLGayColor;
    header.title = model.timeTitle;
    return header;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.001;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YLTitleArrowCell *cell = [tableView dequeueReusableCellWithIdentifier:[YLTitleArrowCell cellID] forIndexPath:indexPath];
    YLContinuationRecordModel *model = self.viewModel.group[indexPath.section];
    cell.contentStr = model.item[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YLContinuationDetailController *detail = [[YLContinuationDetailController alloc] init];
    YLContinuationRecordModel *model = self.viewModel.group[indexPath.section];
    detail.contractCode = model.item[indexPath.row];
    [self.navigationController pushViewController:detail animated:YES];
}

- (instancetype)init
{
    return [self initWithStyle:UITableViewStyleGrouped];
}

@end
